package com.aoxon.cargo.adapter;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.media.ImageReader;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.aoxon.cargo.R;

public class BannerAdapter extends PagerAdapter {

    private List<Drawable> views = new ArrayList<Drawable>();
    private LayoutInflater inflate;
    private ViewPager      group;

    public BannerAdapter(Activity activity, ViewPager group) {
        super();
        this.inflate = LayoutInflater.from(activity);
        views.add(activity.getResources().getDrawable(R.drawable.banner1));
        views.add(activity.getResources().getDrawable(R.drawable.banner2));
        views.add(activity.getResources().getDrawable(R.drawable.banner3));
        ImageView iv = (ImageView) inflate.inflate(R.layout.banner_item, null);
        this.group = group;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE-Integer.MAX_VALUE%views.size();
    }

    // 来判断显示的是否是同一张图片，这里我们将两个参数相比较返回即可
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    // PagerAdapter只缓存三张要显示的图片，如果滑动的图片超出了缓存的范围，就会调用这个方法，将图片销毁
    @Override
    public void destroyItem(ViewGroup view, int position, Object object) {
        view.removeViewInLayout((ImageView) object);
    }

    // 当要显示的图片可以进行缓存的时候，会调用这个方法进行显示图片的初始化，我们将要显示的ImageView加入到ViewGroup中，然后作为返回值返回即可
    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        Log.v("ViewPager", String.valueOf(group.getCurrentItem()%3));
        Log.v("BannerAdapter", String.valueOf(position));
        Log.v("BannerAdapter view position", String.valueOf(position % views.size()));
        MyImageView iv = (MyImageView) inflate.inflate(R.layout.banner_item, null);
        iv.setImageDrawable(views.get(position % views.size()));
        view.addView(iv);
        return iv;
    }
}

@SuppressLint("Instantiatable") class MyImageView extends ImageView {

    public MyImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public MyImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
    
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        setMeasuredDimension(width, width/2);
    }
    
}
